// eventBus.js
import { reactive } from "vue";

const eventBus = {
    events: reactive(new Map()),

    on(event, callback) {
        if (!this.events.has(event)) {
            this.events.set(event, []);
        }
        this.events.get(event).push(callback);
    },

    off(event, callback) {
        if (!this.events.has(event)) return;
        const eventCallbacks = this.events.get(event);
        if (!callback) {
            // 移除所有监听
            this.events.delete(event);
        } else {
            // 移除特定监听
            this.events.set(event, eventCallbacks.filter((cb) => cb !== callback));
        }
    },

    emit(event, payload) {
        if (!this.events.has(event)) return;
        this.events.get(event).forEach((callback) => callback(payload));
    },
};

export default eventBus;
